package cn.yzgy.yzgyoa.ucenter.controller;


import cn.yzgy.yzgyoa.common.annotation.ApiDesc;
import cn.yzgy.yzgyoa.common.crsp.StatusCode;
import cn.yzgy.yzgyoa.common.crsp.Urmf;
import cn.yzgy.yzgyoa.common.utils.ToolsUtil;
import cn.yzgy.yzgyoa.ucenter.dto.SysDepartmentDTO;
import cn.yzgy.yzgyoa.ucenter.entity.SysDepartment;
import cn.yzgy.yzgyoa.ucenter.service.ISysDepartmentService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import cn.yzgy.yzgyoa.common.controller.BaseController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Yqsen
 * @since 2021-06-03
 */
@RestController
@RequestMapping("/ucenter/sys-department")
public class SysDepartmentController extends BaseController {
    private final ISysDepartmentService sysDepartmentService;
    private final ToolsUtil toolsUtil;

    public SysDepartmentController(ISysDepartmentService sysDepartmentService, ToolsUtil toolsUtil) {
        this.sysDepartmentService = sysDepartmentService;
        this.toolsUtil = toolsUtil;
    }

    @ApiDesc("获取部门列表")
    @GetMapping("/getAllDepartment")
    @RequiresPermissions("api:department:data:getAllDepartment")
    public Urmf getAllDepartment() {
        IPage<SysDepartmentDTO> departmentPage = sysDepartmentService.getAllDepartment(new Page<>(1, 2000));
        return Urmf.success(getCurrentMethodName(), departmentPage);
    }

    @ApiDesc("获取部门列表下拉")
    @PostMapping("/getDepartmentTree")
    @RequiresPermissions("api:department:data:getDepartmentTree")
    public String getDepartmentTree() {
        JSONObject departmentJson = toolsUtil.getReturnData("Department");
        List<SysDepartmentDTO> list = sysDepartmentService.getDepartmentTree();
        for (SysDepartmentDTO sysDepartmentDTO : list) {
            sysDepartmentDTO.setTitle(sysDepartmentDTO.getName());
            sysDepartmentDTO.setCheckArr("0");
        }
        departmentJson.put("data",list);
        return departmentJson.toJSONString();
    }

    /**
     * 新增表数据
     * @param queryDto 部门对象
     * @return 成功或失败状态
     */
    @ApiDesc(value = "新增部门数据", log = true)
    @PostMapping("/addDepartment")
    @RequiresPermissions("api:department:data:addDepartment")
    public  Object addDepartment(@RequestBody SysDepartment queryDto){
        boolean state = sysDepartmentService.addDepartment(queryDto,getCurrentUser());
        if (!state) {
            return Urmf.fail(getCurrentMethodName(), "数据添加失败！");
        }
        return Urmf.success(getCurrentMethodName(), "数据添加成功！");
    }

    /**
     * 根据前端传入对象修改数据表内容
     * @param queryDto 部门对象
     * @return 成功状态
     */
    @ApiDesc(value = "修改部门数据内容", log = true)
    @PutMapping("/updateDepartment")
    @RequiresPermissions("api:department:data:updateDepartment")
    public Urmf updateDepartment(@RequestBody SysDepartment queryDto){
        boolean state = sysDepartmentService.updateDepartment(queryDto,getCurrentUser());
        if (!state) {
            return Urmf.fail(getCurrentMethodName(), StatusCode.FAILED, "修改部门数据内容失败！");
        }
        return Urmf.success(getCurrentMethodName(), StatusCode.SUCCESS,"数据修改成功");
    }

    /**
     * 级联删除部门数据
     * @param queryDto 部门对象
     * @return 成功状态
     */
    @ApiDesc(value = "级联删除部门数据内容", log = true)
    @DeleteMapping("/deleteDepartment")
    @RequiresPermissions("api:department:data:deleteDepartment")
    public Object deleteDepartment(@RequestBody SysDepartment queryDto){
        boolean state = sysDepartmentService.deleteDepartment(queryDto.getId());
        if (!state) {
            return Urmf.fail(getCurrentMethodName(), StatusCode.FAILED, "修改部门数据内容失败！");
        }
        return Urmf.success(getCurrentMethodName(), StatusCode.SUCCESS,"数据修改成功");
    }
}
